﻿@model string
@inject IProductService productService
@inject IPictureService pictureService
@using Nop.Core;
@using Nop.Services.Media
@using Nop.Web.Framework.UI;
@using Nop.Services.Catalog;
@using Newtonsoft.Json;
@using Nop.Web.Areas.Admin.Models.Catalog;
@{
    //register CSS and JS
    Html.AddCssFileParts("~/lib/fineuploader/fine-uploader.min.css");
    Html.AddScriptParts("~/lib/fineuploader/jquery.fine-uploader.min.js");

    //other variables
    var random = CommonHelper.GenerateRandomInteger();
    var clientId = "video" + random;
    ProductVideoModel video=null;
    if(!string.IsNullOrEmpty(Model))
    {
        video = JsonConvert.DeserializeObject<ProductVideoModel>(Model);
    }
    var coverUrl = video==null?pictureService.GetDefaultPictureUrl(100):video.CoverUrl;
    var videoUrl= video==null?"#":video.VideoUrl;
}

<div id="@(clientId + "value")">
    <input type="hidden" asp-for="@Model" />

</div>
<div class="row">
    <div class="col-md-12">
        <div id="@(clientId + "image")" class="uploaded-video">
            <a href="@(videoUrl)"  target='_blank'><img src="@(coverUrl)" width="100" /></a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        @*fine uploader container*@
        <div id="@clientId" class="upload-image-button pull-left margin-t-5">
            <noscript>
                <p>Please enable JavaScript to use file uploader.</p>
            </noscript>
        </div>
    </div>
</div>

@*fine uploader template (keep it synchronized to \Content\fineuploader\templates\default.html)*@
<script type="text/template" id="@(clientId)-qq-template">
    <div class="qq-uploader-selector qq-uploader">
        <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
            <span>@T("Common.FileUploader.DropFiles")</span>
        </div>
        <div class="qq-upload-button-selector qq-upload-button">
            <div>@T("Common.FileUploader.Upload")</div>
        </div>
        <span class="qq-drop-processing-selector qq-drop-processing">
            <span>@T("Common.FileUploader.Processing")</span>
            <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
        </span>
        <ul class="qq-upload-list-selector qq-upload-list">
            <li>
                <div class="qq-progress-bar-container-selector">
                    <div class="qq-progress-bar-selector qq-progress-bar"></div>
                </div>
                <span class="qq-upload-spinner-selector qq-upload-spinner"></span>
                <span class="qq-edit-filename-icon-selector qq-edit-filename-icon"></span>
                <span class="qq-upload-file-selector qq-upload-file"></span>
                <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
                <span class="qq-upload-size-selector qq-upload-size"></span>
                <a class="qq-upload-cancel-selector qq-upload-cancel" href="#">@T("Common.FileUploader.Cancel")</a>
                <a class="qq-upload-retry-selector qq-upload-retry" href="#">@T("Common.FileUploader.Retry")</a>
                <a class="qq-upload-delete-selector qq-upload-delete" href="#">@T("Common.FileUploader.Delete")</a>
                <span class="qq-upload-status-text-selector qq-upload-status-text" id="@(clientId)-qq-uploader-status"></span>
            </li>
        </ul>
    </div>
</script>
<script>
    $(document).ready(function() {
        $("#@(clientId)").fineUploader({
            request: {
                endpoint: '@(Url.Content("~/Admin/Video/AsyncUpload"))'
            },
            template: "@(clientId)-qq-template",
            multiple: false
        }).on("complete", function(event, id, name, responseJSON, xhr) {
            if (responseJSON.success) {
                 
                $("#@(clientId + "image")").html("<a href='" + responseJSON.videoUrl + "' target='_blank'><img src='" + responseJSON.coverUrl + "' width=100/></a>");
                $("#@(clientId + "value") input").val(JSON.stringify(responseJSON));
            } else {
                $("#@(clientId + "-qq-uploader-status")").text(responseJSON.message);
            }
        });

    });
</script>